package cn.gedobu.some.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MirrorMeter {
    public static String run()
    {
        String[] addrs= {
            "mirrors.ustc.edu.cn",
            "mirrors.aliyun.com"
        };

        String fastSite = addrs[0];
        int fastSpeed = 0;

        if (addrs.length < 1)
        {
            System.out.println("syntax Error!");
        }
        else
        {
            for(int i=0;i<addrs.length;i++){
                String line = null;
                try
                {
                    Process pro = Runtime.getRuntime().exec("ping " + addrs[i]+" -l 1000 -n 4");
                    BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                    while((line = buf.readLine()) != null){
                        int position=0;
                        if((position=line.indexOf("Average"))>=0)
                        {
                            System.out.println(line);
                            String value = line.substring(position+10,line.lastIndexOf("ms"));
                            int speed = (1000/Integer.parseInt(value));
                            System.out.println("your speed is: "+speed+" KB");

                            if ( fastSpeed < speed ) {
                                fastSite = addrs[i];
                            }
                        }
                    }
                }
                catch(Exception ex)              {
                    System.out.println(ex.getMessage());
                }
            }
        }

        return fastSite + "/eclipse";
    }
}